package com.wonderful.eyepetizer.ui.pages.person

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.view.WindowInsetsControllerCompat
import coil.compose.rememberAsyncImagePainter
import com.wonderful.eyepetizer.R
import com.wonderful.eyepetizer.data.entity.bean.ItemData
import com.wonderful.eyepetizer.ui.pages.detail.VideoDetailActivity
import com.wonderful.eyepetizer.ui.theme.Black_54
import com.wonderful.eyepetizer.ui.theme.Black_87
import com.wonderful.eyepetizer.utils.DateUtils
import com.wonderful.eyepetizer.view.Swipe
import com.wonderful.eyepetizer.view.TitleBarWidget
import com.wonderful.eyepetizer.view.rememberSwipeState
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/2/26 14:10
 */
@AndroidEntryPoint
class HistoryWatchActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.statusBarColor = android.graphics.Color.WHITE
        WindowInsetsControllerCompat(window, window.decorView).apply {
            isAppearanceLightStatusBars = true
        }
        setContent {
            HistoryWatchPage()
        }
    }

    companion object {
        fun actionStart(context: Context) {
            val intent = Intent(context, HistoryWatchActivity::class.java)
            context.startActivity(intent)
        }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HistoryWatchPage(viewModel: PersonViewModel = androidx.lifecycle.viewmodel.compose.viewModel()) {
    LaunchedEffect(key1 = Unit, block = {
        viewModel.getVideoList()
    })
    val coroutineScope = rememberCoroutineScope()
    val context = LocalContext.current

    Column {
        TitleBarWidget(
            title = stringResource(id = R.string.watch_record), showNavigationIcon = true
        ) {
            if (context is Activity) context.finish()
        }
        LazyColumn {
            items(viewModel.videoList.size) {
                val itemData = viewModel.videoList[it]
                VideoItemWidget(modifier = Modifier.animateItemPlacement(),
                    itemData = itemData,
                    scope = coroutineScope,
                    onDelete = {
                        viewModel.deleteVideo(itemData)
                    })
            }
        }
    }
}

@Composable
fun VideoItemWidget(
    modifier: Modifier = Modifier,
    itemData: ItemData,
    scope: CoroutineScope,
    onDelete: (itemData: ItemData) -> Unit
) {
    val context = LocalContext.current
    val swipeState = rememberSwipeState()
    Swipe(modifier = modifier, state = swipeState, background = {
        Box(
            modifier = Modifier
                .width(66.dp)
                .fillMaxHeight()
                .background(Color.Red)
                .clickable {
                    scope.launch { swipeState.close() }
                    onDelete(itemData)
                }, contentAlignment = Alignment.Center
        ) {
            Text(text = "删除", color = Color.White)
        }
    }) {
        Row(
            modifier = Modifier
                .padding(start = 10.dp, top = 10.dp, end = 10.dp, bottom = 5.dp)
                .clickable {
                    VideoDetailActivity.actionStart(context, itemData)
                }
        ) {
            Box {
                Image(
                    painter = rememberAsyncImagePainter(model = itemData.cover.detail),
                    contentDescription = null,
                    modifier = Modifier
                        .width(135.dp)
                        .height(80.dp)
                        .clip(RoundedCornerShape(5.dp)),
                    contentScale = ContentScale.Crop
                )
                Text(
                    text = DateUtils.formatDateMsByMS(itemData.duration.toLong()),
                    style = TextStyle(
                        color = Color.White,
                        fontSize = 10.sp,
                        fontWeight = FontWeight.Bold
                    ),
                    modifier = Modifier
                        .padding(end = 5.dp, bottom = 5.dp)
                        .clip(RoundedCornerShape(5.dp))
                        .background(Black_54)
                        .padding(3.dp)
                        .align(Alignment.BottomEnd)
                )
            }
            Column(
                modifier = Modifier
                    .padding(10.dp)
                    .weight(1f)
            ) {
                Text(
                    text = itemData.title,
                    style = TextStyle(
                        color = Black_87,
                        fontSize = 14.sp,
                        fontWeight = FontWeight.Bold
                    )
                )
                Text(
                    text = "#${itemData.category} / ${itemData.author?.name}",
                    style = TextStyle(color = Black_87, fontSize = 12.sp),
                    modifier = Modifier.padding(top = 15.dp)
                )
            }
        }
    }
}